<template>
	<div> 
		<van-nav-bar
			:title=msg
			left-text="返回"
			left-arrow
			@click-left="goBackFn"
			/>

		<van-field
			v-model="username"
			required
			clearable
			label="用户名"
			placeholder="请输入用户名"
			@click-icon="username=''"
			/>

		<van-field
			v-model="password"
			type="password"
			required
			clearable
			label="密码"
			placeholder="请输入密码"
			@click-icon="username=''"
			/>

		<van-button type="danger" @click="userLoginFn">登录</van-button>
		<van-button type="danger" @click="registerBtn">注册</van-button>
		<footerBar></footerBar>

	</div>
</template>

<script>
import axios from 'axios'
import API_LIST from '@/APIList.config'
import footerBar from './footerBar'

export default{
	name:'userLogin',
	data(){
		return {
			msg:'登录页面',

			username:'',
			password:''
		}
	},
	components:{footerBar},
	methods:{

		// 注册
		registerBtn(){
			this.$router.push({path:'/register'});
		},
		// 回到上一步
		goBackFn(){
			this.$router.go(-1);
		},
		userLoginFn(){
			let _loginObj = {
				u : this.username,
				p : this.password
			}
			// console.log( _loginObj );
			axios.post( API_LIST.userLogin_post, _loginObj )
				.then( _d=>{
					//console.log(_d.data );
					if(_d.data.reg_code === 3 ){
						
						//localStorage,不能存储对象，要转成字符串再存
							localStorage.userName = JSON.stringify(_d.data.regInfo);
							this.$dialog.alert({
								message :'欢迎您：'+ _d.data.regInfo.username +'，登录成功'})
								.then(()=>{
									this.$router.push({path:'/'},()=>{
										localStorage.setItem('inxState',0);
					});
								})
						}else{
							this.$dialog.alert({
								message : _d.data.regInfo});
								this.userLoginResetFn();
						}
					})
					
				
		},
		//重置方法
		userLoginResetFn(){
			this.username='',
			this.password=''
		}
	}
}
</script>